package com.zoweunion.mechanic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.UserCouponDao;
import com.zoweunion.mechanic.dao.base.MessageRecordDao;
import com.zoweunion.mechanic.entity.UserCouponEntity;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.util.*;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

import com.zoweunion.mechanic.dao.PromotionCouponDao;
import com.zoweunion.mechanic.entity.PromotionCouponEntity;
import com.zoweunion.mechanic.service.PromotionCouponService;

import static java.util.stream.Collectors.toList;


@Service("promotionCouponService")
public class PromotionCouponServiceImpl extends ServiceImpl<PromotionCouponDao, PromotionCouponEntity> implements PromotionCouponService {

    @Autowired
    private PromotionCouponDao promotionCouponDao;
    @Autowired
    private UserCouponDao userCouponDao;
    @Autowired
    private BaseService baseService;
    @Autowired
    private MessageRecordDao messageRecordDao;
    @Autowired
    private JiGuangPushUtil jiGuangPushUtil;


    @Override
    public PageInfo queryPage(Map<String, Object> map)  {
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);
        List<PromotionCouponEntity> mapList = promotionCouponDao.queryPage(map);
        PageInfo<PromotionCouponEntity> mapPageInfo = new PageInfo<>(mapList);
        return mapPageInfo;
    }

    @Override
    public void promotionCouponInsertUpdate(PromotionCouponEntity params) {
        promotionCouponDao.insert(params);
        List<Map<String,Object>> userIds = promotionCouponDao.selectUserId();
        for(int i= 0; i<params.getSheet();i++) {
           userIds.forEach(item->{
               userCouponDao.insert(UserCouponEntity.builder()
                       .couponId(params.getId())
                       .userId(Orgin.getString(item,"id"))
                       .grantTime(new Date())
                       .createTime(new Date())
                       .build());
           });
        }
        userIds.parallelStream().forEach(item->{
            String mobilePhone = Orgin.getString(item, "mobile_phone");
            String id = Orgin.getString(item, "id");
            SmsUtil.sendSms(mobilePhone,
                    params.getNoteTemplate());
            //推送通知
            Map<String, Object> map1 = new HashMap<>();
            map1.put("s_id","");
            map1.put("receive_user",id);
            map1.put("client","YD");
            map1.put("id", UuidUtil.get32UUID());
            String pushTime = DateUtil.formatTime(LocalDateTime.now());
            String content = String.format(SmsUtil.couponpush, params.getCouponMoney());
            Map<String, Object> messageRecord = baseService.pushMessageCoupon(id,
                    pushTime, content);
            jiGuangPushUtil.publishMessagePush(messageRecord, "", "", id);
            map1.put("context", content);
            map1.put("send_type",20);
            messageRecordDao.addMessageRecord(map1);

        });
    }

    @Override
    public List<PromotionCouponEntity> couponList(Map<String, Object> params) {
        return promotionCouponDao.couponList(params);
    }

    @Override
    public List<PromotionCouponEntity> couponListByDate(Map<String, Object> params) {
        return promotionCouponDao.couponList(params);
    }

    @Override
    public PromotionCouponEntity couponRecommend(Map<String, Object> params) {
        List<PromotionCouponEntity> entities = promotionCouponDao.couponList(params);
        double payMoney = Orgin.getDoubleValue(params, "payMoney");
        entities.parallelStream().forEach(item->{
            if (item.getEmployStatus()==1){
                if (payMoney>item.getCouponMoney().doubleValue()){
                    item.setMinMoney(payMoney-item.getCouponMoney().doubleValue());
                } else {
                    item.setMinMoney((double) 0);
                }
            }else if (item.getEmployStatus()==2){
                double money = Math.floor(payMoney / item.getReduceMoney()) *
                        item.getCouponMoney();
                if (payMoney>money){
                    item.setMinMoney(payMoney-money);
                }
            }
        });
        List<PromotionCouponEntity> collect = entities.stream().filter(x -> x.getMinMoney() != null && x.getMinMoney() != payMoney)
                .sorted(Comparator.comparing(PromotionCouponEntity::getMinMoney))
                .collect(toList());
        if (collect.size()>0){
            return collect.get(0);
        }else {
            return null;
        }

    }

    @Override
    public List<PromotionCouponEntity> couponListNotLogin(Map<String, Object> params) {
        return promotionCouponDao.couponListNotLogin(params);
    }

}
